//
//  SONG.m
//  Khmer media
//
//  Created by Rainseylong on 2/11/13.
//  Copyright (c) 2013 imer. All rights reserved.
//

#import "SONG.h"
#import "Apiconnection.h"

static NSString *const TopDownloadSong=@"Productions/productionsJson/?encryptKey=g2cSBfct1uaGThCKWcHejter7eqlEXz3P14V7JewTao=&phoneModel=&menufacturer=samsung&phoneNumber=&osVersion=4.0.3&deviceID=IML74K";
static NSString *const SongsNormalMain=@"Songs/songsJson/";
static NSString *const SongsNormalLast=@"/g2cSBfct1uaGThCKWcHejter7eqlEXz3P14V7JewTao";

@implementation SONG


@synthesize songId=_songId;
@synthesize albumeId=_albumeId;
@synthesize countListening=_countListening;
@synthesize dateCreated=_dateCreated;
@synthesize dateUpdated=_dateUpdated;
@synthesize description=_description;
@synthesize songUrl=_songUrl;
@synthesize songTitle=_songTitle;
@synthesize songTitleRendered=_songTitleRendered;
@synthesize songArtist=_songArtist;
@synthesize songAlbum=_songAlbum;
@synthesize songYear=_songYear;
@synthesize songComment=_songComment;
@synthesize songTrack=_songTrack;
@synthesize songGenre=_songGenre;
@synthesize songBitrate=_songBitrate;
@synthesize songSubtitle=_songSubtitle;
@synthesize songBand=_songBand;
@synthesize songLength=_songLength;
@synthesize fileSize=_fileSize;


- (id)initWithAttributes:(NSDictionary *)attributes
{
    self = [super init];
    if (!self) {
        return nil;
    }
    _songId=[attributes objectForKey:@"songId"];
    _albumeId=[attributes objectForKey:@"albumId"];
    _countListening=[attributes objectForKey:@"countListening"];
    _dateCreated=[attributes objectForKey:@"dateCreated"];
    _dateUpdated=[attributes objectForKey:@"dateUpdated"];
    _description=[attributes objectForKey:@"description"];
    _songUrl=[attributes objectForKey:@"url"];
    _songTitle=[attributes objectForKey:@"title"];
    _songTitleRendered=[attributes objectForKey:@"titleRendered"];
    _songArtist=[attributes objectForKey:@"artist"];
    _songAlbum=[attributes objectForKey:@"album"];
    _songYear=[attributes objectForKey:@"year"];
     _songComment= [[attributes objectForKey:@"comment"] isKindOfClass:[NSNull class]] ? @""
     : [attributes objectForKey:@"comment"];
     
    _songTrack=[attributes objectForKey:@"track"];
    _songGenre=[attributes objectForKey:@"genre"];
    _songBitrate=[attributes objectForKey:@"bitrate"];
    _songSubtitle=[attributes objectForKey:@"subtitle"];
    _songBand=[attributes objectForKey:@"band"];
    _songLength=[attributes objectForKey:@"length"];
    _fileSize=[[attributes objectForKey:@"filesize"] floatValue];
    
    return self;
}

+(void)convertSongsToOBJectWithBlock:(void (^) (NSArray *songsArray, NSError *error))block
                        WithSongType:(BOOL)songType
                            ablumeID:(int)albumeID
{
    //SongType == YES : topdownload / NO : normal Songs
    NSString *songURL=songType ==YES ? TopDownloadSong : [NSString stringWithFormat:@"%@%d%@",SongsNormalMain,albumeID,SongsNormalLast];
    
    [[Apiconnection sharedClient] getPath:songURL parameters:Nil success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSMutableArray *tempDic=[responseObject valueForKey:@"responseBody"];
         NSMutableArray *mutablePosts = [NSMutableArray arrayWithCapacity:tempDic.count];
         
         for (int i=0;i < tempDic.count;i++) {
             SONG *post = [[SONG alloc] initWithAttributes:[tempDic objectAtIndex:i]];
             [mutablePosts addObject:post];
         }
         if (block) {
             block([NSArray arrayWithArray:mutablePosts], nil);
         }
         
     } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         if (block) {
             block([NSArray array], error);
         }
     }];
}

@end
